package itsm.isperp.framework.web.servlet;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.domain.FileUploadInfo;
import itsm.isperp.framework.domain.UploadInfo;
import itsm.isperp.framework.service.UploadFileService;
import itsm.isperp.framework.upload.FilePersister;
import itsm.isperp.framework.utils.FileUploadParser;
import itsm.isperp.framework.utils.HibernateUUIDGenerator;
import itsm.isperp.framework.utils.JsonBuilder;
import itsm.isperp.module.entity.app.AppUploadFile;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean;

/**
 * 用于上传文件在一个服务程序
 * 
 * @author lizx
 * @date 2013-3-14
 */
public class UploadFileServlet extends HttpServlet implements InitializingBean {

	/**	
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected UploadFileService<UploadInfo> uploadFileService;

	private FilePersister filePersister;

	public UploadFileServlet() {

	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		resp.setCharacterEncoding("UTF-8");

		FileUploadParser parser = new FileUploadParser();
		FileUploadInfo fileUploadInfo = parser.parse(req);
		String sourceId = req.getParameter("sourceId");

		// resp.setContentType("application/json");

		for (FileItem fileItem : fileUploadInfo.getFileItems()) {
			if (fileItem.getContentType() != null) {// 如果是文件类型

				// this.uploadInfo = new AppUploadFile();
				// this.uploadInfo.setId(null);

				// //将文件进行保存到目录在持久化操作

				UploadInfo uploadInfo = this.getUploadInfo();

				uploadInfo.setId(HibernateUUIDGenerator.generate());
				uploadInfo.setSourceId(sourceId);
				fileUploadInfo.parseUploadInfo(fileItem.getName(), uploadInfo);

				filePersister.persist(fileItem.getInputStream(), uploadInfo);
				if (StringUtils.isNotEmpty(sourceId)) {
					// 记录文件删除在信息到数据库中
					uploadFileService.saveOrUpdate(uploadInfo);
				}
				String s = JsonBuilder.buildNormalBinder().toJson(uploadInfo);
				resp.getWriter().write(s);

			}

		}
	}

	/**
	 * 初始化
	 */
	@Override
	public void init(ServletConfig servletconfig) throws ServletException {
		super.init(servletconfig);

		if (this.filePersister == null) {
			this.filePersister = ContextHolder
					.getSpringBean("isperp.filePersister");
		}

		if (this.uploadFileService == null) {
			this.uploadFileService = ContextHolder
					.getSpringBean("appUploadFileService");// 此处可设置为可配置

		}
	}

	/**
	 * 初始化
	 */
	@Override
	public void init() throws ServletException {
		super.init();
		// 通过Context设置系统参数

		if (this.filePersister == null) {
			this.filePersister = ContextHolder
					.getSpringBean("isperp.filePersister");
		}

		if (this.uploadFileService == null) {
			this.uploadFileService = ContextHolder
					.getSpringBean("appUploadFileService");// 此处可设置为可配置

		}

	}

	@Override
	public void afterPropertiesSet() {
		if (this.filePersister == null) {
			throw new IllegalArgumentException(
					"Property 'filePersister' is required");
		}

	}

	public UploadInfo getUploadInfo() {
		return new AppUploadFile();
	}

	public FilePersister getFilePersister() {
		return filePersister;
	}

	public void setFilePersister(FilePersister filePersister) {
		this.filePersister = filePersister;
	}

}
